home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / rmdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-24  |  1.8 KB  |  77 lines

  1. /* rmdir -- remove a directory */
  2. /* written by Eric R. Smith and placed in the public domain */
  3.  
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <support.h>
  7. #include <osbind.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include "symdir.h"
  11. #include "lib.h"
  12.  
  13. /*
  14.  * BUG(?): rmdir(foo) will unlink foo even if it is a symbolic link to a
  15.  * non-directory file (unless it's an automatic link, in which case the
  16.  * rmdir works iff the actual directory is removed).
  17.  */
  18.  
  19. int rmdir(_path)
  20.     const char *_path;
  21. {
  22.     SYMDIR *dir = 0;
  23.     SYMENTRY *ent = 0;
  24.     char path[FILENAME_MAX], *s;
  25.     int  r, nm_change;
  26.  
  27.     nm_change = _unx2dos(_path, path);
  28. /*
  29.  * _unx2dos returns _NM_LINK and sets __link_path for symbolic links
  30.  */
  31.  
  32.     if (nm_change == _NM_LINK) {    /* _path was to a symbolic link */
  33.         dir = _read_symdir(__link_path);
  34.         if (!dir || !(ent = _symdir_lookup(dir, __link_name)))
  35.             return -1;
  36.     }
  37. /* if symbolic links are active, insist that names match */
  38.     else if (_lOK && nm_change == _NM_CHANGE) {
  39.         errno = ENOENT;
  40.         return -1;
  41.     }
  42. /*
  43.  * delete path if _path was not a symlink, or if it was an automatic
  44.  * symlink.
  45.  */
  46.     r = (!ent || (ent->flags & SD_AUTO));
  47.     if (dir) _free_symdir(dir);
  48.     if (r) {
  49. /*
  50.  * remember to delete the ".dir" file in the directory as well; the user
  51.  * shouldn't have to do this. _read_symdir(path) also removes "path"
  52.  * from the symbolic directory cache.
  53.  */
  54.         dir = _read_symdir(path);
  55.         if (dir) {
  56.             for (s = path; *s; s++) ;
  57.             *s = '\\';
  58.             strcpy(s+1, _lDIR);    /* append ".dir" */
  59.             (void)Fdelete(path);
  60.             *s = 0;    /* restore name */
  61.         }
  62.         r = Ddelete(path);
  63.         if (r < 0 && dir)
  64.           /* rewrite symbolic directory if dir was nonempty */
  65.           (void)_write_symdir (path, dir);
  66.     }
  67.     if (r < 0) {
  68.         errno = -r;
  69.         r = -1;
  70.     }
  71.     else if (ent) {
  72. /* unlink symbolic link */
  73.         r = unlink(_path);
  74.     }
  75.     return r;
  76. }
  77.